package chess;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.border.Border;

public class MyMouseListener implements MouseListener {
	private Board board;
	private Border redLine = BorderFactory.createLineBorder(Color.RED, 2, true);
	private Border blueLine = BorderFactory.createLineBorder(Color.BLUE, 4,
			false);
	/*private Border greenLine = BorderFactory.createEtchedBorder(3, Color.GREEN,
			Color.blue);*/
	private Border empty = BorderFactory.createEmptyBorder();
	private ImagePanel target = null;
	private ImagePanel selected = null;
	int turn = -2 ; 
	int team = -1;// 0: team A, 1 : team B
	public int getTeam() {
		return team;
	}

	public void setTeam(int team) {
		this.team = team;
	}

	public MyMouseListener(Board board) {
		// TODO Auto-generated constructor stub
		this.board = board;
	}

	@Override
	public void mouseClicked(MouseEvent e) {

		ImagePanel piece = (ImagePanel) e.getSource();
		System.out.println(piece.getStringPosition());
		if (selected == null) {
			if (piece.getChessman() != null
					&& piece.getChessman().getTeam() == team && turn == team) {

				selected = piece;
				piece.setBorder(redLine);
				for (int i = 0; i < piece.getChessman()
						.canMove(board.getArrayChess()).size(); i++) {
					System.out.println("co the :"
							+ piece.getChessman()
									.canMove(board.getArrayChess()).get(i));
				}
			}
		} else {
			if (piece != selected) { // neu nhan lai o da click thi khong gan
										// target va huy luon selected
				target = piece;
				// target.setBorder(greenLine);

				if (board.moveChessman(selected, target)) {
					selected = null;
					target = null;
				
				} else {
			
					target = null;
					
				}
			} else {
				selected.setBorder(empty);
				selected = null;

			}
		}
	}

	public int getTurn() {
		return turn;
	}

	public void setTurn(int turn) {
		this.turn = turn;
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		ImagePanel piece = (ImagePanel) e.getSource();
		if (piece != selected)
			piece.setBorder(blueLine);
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		ImagePanel piece = (ImagePanel) e.getSource();
		if (piece != selected)
			piece.setBorder(empty);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		/*
		 * ImagePanel piece = (ImagePanel)e.getSource();
		 * if(piece.getComponents() != null){ //piece.setBorder(redLine); JLabel
		 * s = (JLabel)piece.getComponent(1);
		 * 
		 * System.out.println(s.getName()+":"+piece.getChessman());
		 * 
		 * ArrayList<String> moves = piece.getChessman().canMove(); for (int i =
		 * 0; i < moves.size(); i++) { System.out.println("moves :"+
		 * moves.get(i)); board.enablePieceBorder(moves.get(i)); } }
		 */

		// board.setIsSelect(piece.getChessman());
		// select o co quan co thi gan cho selected

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		/*
		 * JPanel piece = (JPanel)e.getSource(); if(piece.getComponents() !=
		 * null){ piece.setBorder(empty); board.disableAllPieceBorder(); }
		 */

	}

}
